<!DOCTYPE html>

<html>
	<head>
		<meta charset="utf-8" />
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>Accueil</title>
	</head>

	<body>
		<?php
		session_start();
	?>

	<div class="top-menu">
		<ul>
			<li><a href="index.php" >Accueil</a></li>
			<li><a href="affCategorie.php">Catégories</a></li>
		</ul>
		
		<?php
		if (!isset($_SESSION) || !isset($_SESSION['connected']) || !$_SESSION['connected'] == true)
		{
			?>
		<div class="login-box">
		<form action="connexion.php" method="post">
			Connexion :<input type="text" name="login" placeholder="Login" />
			<input type="text" name="mdp" placeholder="Password" />
			<input type="submit" value="OK">
		</form>
		</div>
		<?php
		}
		else
		{
			?>
			<div class="login-box">Vous etes connecté en tant que <?php
			if (isset($_SESSION) && isset($_SESSION["login"]))
				echo '<div class="login-name">' . $_SESSION["login"] . '</div>';
			?>
			<a href="sdestr.php">Déconnexion</a>
			</div>
			<?php
		}
		?>
		<br />
	</div>
	
		<?php

			if (isset($_GET['idSuj']))
				{
 					$idSuj = $_GET['idSuj'];
 					if (isset($_GET['page']))
 						{
 							$page = $_GET['page'];
						}
					else 
						{
							$page = 1;
						}
				}
			else 
				{
					?>

						<script type="text/JavaScript">
							document.location.href="index.php";
						</script>									
					
					<?php
				}

			$mysqli = new mysqli("localhost", "root", "root", "forum");
			
			/* Vérification de la connexion */
			if (mysqli_connect_errno())
				{
    				echo 'Échec de la connexion : ' . mysqli_error();
    				exit;
				}

			$requete = 'SELECT * FROM message WHERE id_sujet = \'' . $idSuj . '\';';
			if ($response = $mysqli->query($requete))
				{
					$cpt = 0;
					while ($row = $response->fetch_assoc())
						{
							$arrayToAff[$cpt] = $row;
							$cpt++;
                        }
					$maxPage = ($cpt / 20) + 1;
					$cpt = 0;
					$toAffMin = ($page - 1) * 20;
					$toAffMax = ($page * 20) - 1;
					$toAff = $toAffMin;
					while (isset($arrayToAff[$toAff]) && $arrayToAff[$toAff] != null && $cpt < 20)
						{
								echo '<div class="postbox">
								<div class="postbox-title">';
                                $idPost = $arrayToAff[$toAff]['id_message'];
                                $requete1 = 'SELECT titre_cat FROM categorie,sujet,message WHERE categorie.id_cat = sujet.id_cat AND sujet.id_sujet = message.id_sujet AND id_message = ' . $idPost . ';';
                                $requete2 = 'SELECT titre_sujet FROM sujet,message WHERE sujet.id_sujet = message.id_sujet AND id_message = ' . $idPost . ';';
                                $requete3 = 'SELECT titre_message FROM message WHERE id_message = ' . $idPost . ';';
                                $requete4 = 'SELECT login FROM message,user WHERE message.id = user.id AND id_message = ' . $idPost . ';';
                                $result1 = $mysqli->query($requete1);
                                $result2 = $mysqli->query($requete2);
                                $result3 = $mysqli->query($requete3);
                                $result4 = $mysqli->query($requete4);
                                echo $result1->fetch_array()[0] . " -> " . $result2->fetch_array()[0] . " -> " . $result3->fetch_array()[0] . " -> " . $result4->fetch_array()[0];
                        
                        
								if (isset($_SESSION) && isset($_SESSION['connected']) && $_SESSION['connected'] == true && ($_SESSION['isAdmin'] || $_SESSION['isModo']))
								{
									echo '<a href="delete_post.php?message=' . $idPost . '">Supprimer le post</a>';	
                                }
								echo '</div>
									<div class="postbox-avatar_box">
									<img src="ressources/images/NoAvatar.png" />
									<div class="postbox-avatar_box-info">';
                                            $requete = 'SELECT login FROM message,user WHERE message.id = user.id AND id_message = ' . $idPost . ';';
                                            $result = $mysqli->query($requete);
                                            echo "Utilisateur : " . $result->fetch_array()[0] . "<br />";

                                            $requete = 'SELECT COUNT(*) AS var_count FROM message,user WHERE message.id = user.id AND login = (SELECT login FROM message,user WHERE message.id = user.id AND id_message = ' . $idPost . ')';
                                            $result = $mysqli->query($requete);
                                            echo "Messages : " . $result->fetch_assoc()['var_count'] . "<br />";

                                            $requete = 'SELECT dateInscription FROM message,user WHERE message.id = user.id AND id_message = \'' . $idPost . '\';';
                                            if (!$result = $mysqli->query($requete))
                                                echo mysqli_error($mysqli);
                                            echo "Date d'inscription : " . $result->fetch_array()[0] . "<br />";

                                            $requete = 'SELECT date_message FROM message WHERE id_message = ' . $idPost . ';';
                                            $result = $mysqli->query($requete);
                                            echo "Date du message : " . $result->fetch_array()[0] . "<br />";
                                        
								echo '</div>
							</div>
							<div class="postbox-frame">
							<div class="postbox-text">';
							echo $arrayToAff[$toAff]['contenu_message'];
							echo '</div>
						</div>
					</div>';
							
							/**/
							$cpt++;
							$toAff++;
						}
					$cpt = 1;

					echo '<div class="core-design"><form action="putMessage.php?idSuj=' . $idSuj . '" method="post">
							<p>Titre : <input type="text" name="titre" /></p>
							<p>Message : <input type="text" name="message" /></p>
							<p><input type="submit" value="OK"></p>
							</form></div>';

					echo '<footer>Page' . $page . ' / ' . intval($maxPage) . ' ';
					while ($cpt < $maxPage)
					{
						echo '<a href="affPost.php?page=' . $cpt . '&idSuj=' . $idSuj . '">' . $cpt . ' </a>';
						$cpt++;
					}
					echo '</footer>';
				}
			else 
				{
					?>

						<script type="text/JavaScript">
							document.location.href="index.php";
						</script>									
					
					<?php
				}
		
		?>
	</body>
</html>